<template>
  <div>
    <FormPanel ref="topFilter" :list="topFilterList" :isSubmitBtn="false"></FormPanel>
    <el-row>
      <el-col :span="24">
        <BreakSpace label="调拨单明细" :containerStyle="{ marginBottom: '20px' }"></BreakSpace>
        <div class="grid-content bg-purple">
          <FilterTable
            height="auto"
            ref="table"
            columnsRef="allocationOrderItemsRef"
            :columns="columns"
            :fetchapi="fetchapi"
            :params="fetchParams"
            :datakey="'rows'"
            :onSyncTableData="handleSyncTableData"
            :onCellChange="changeAmount"
            :onColumnsChange="columns => (this.columns = columns)"
          >
          </FilterTable>
        </div>
      </el-col>
      <el-col :span="24">
        <BreakSpace label="审核记录" :containerStyle="{ marginBottom: '20px' }"></BreakSpace>
        <div class="grid-content bg-purple">
          <FilterTable
            height="auto"
            ref="table1"
            columnsRef="checkLogsRef"
            :columns="columns1"
            :fetchapi="fetchapi1"
            :params="fetchParams1"
            :datakey="'rows'"
            :onSyncTableData="handleSyncTableData"
            :onCellChange="changeAmount"
            :onColumnsChange="columns => (this.columns = columns)"
          >
          </FilterTable>
        </div>
      </el-col>
    </el-row>
    <div class="btnWrap">
      <el-button @click="closeDrawer(1)">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction } from '@/utils';
import { getUser, getUserId } from '@/assets/js/auth';
import { saveAllocationIn, queryAllocationIn, queryAllocationInItems, getExaminedLogs } from '@/api/part/allocation';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';
import { exportExcelFile, arrayObjDeWeight } from '@/utils/index';
import moment from 'moment';
import { jsonToHump } from '@/utils/index';

export default {
  mixins: [dictionary],
  props: {
    data: Object
  },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      columns1: this.createTableColumns1(),
      fetchapi1: getExaminedLogs,
      fetchParams1: { xhrAbort: true },
      isShowAddPart: false,
      fetchapi: queryAllocationInItems,
      kehuxuanze: '',
      isShowSearchCustomer: false,
      fetchParams: { xhrAbort: true },
      isShowSearchOldAllcation: false,
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    console.log('data', this.data);
    this.getAllUser();
    this.init();
  },
  methods: {
    createTableColumns1() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '审核日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        },
        {
          title: '审核人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '审核状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('9031'),
          dictItems: this.createDictList('9031'),
          showOverflowTooltip: true
        },
        {
          title: '审核意见',
          dataIndex: 'examinedRemark',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    },
    addPart() {},
    delete(val) {
      this.$refs.table.EXECUTE_DELETE(val);
    },
    confirmSearchCustomer() {},
    async closeDrawer(val) {
      this.$emit('close', false);
    },
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '入库单号',
          disabled: true,
          initialValue: '',
          fieldName: 'allocateInNo'
        },
        {
          type: 'INPUT',
          label: '对方经销商',
          initialValue: '',
          disabled: true,
          fieldName: 'customerCode'
        },
        {
          type: 'INPUT',
          label: '原单据号',
          initialValue: '',
          disabled: true,
          fieldName: 'oldAllocateOutNo'
        },
        {
          type: 'SELECT',
          label: '经办人',
          disabled: true,
          initialValue: '',
          fieldName: 'handler',
          disabled: true,
          itemList: []
        },
        {
          type: 'INPUT',
          label: '经办人电话',
          initialValue: '',
          fieldName: 'handlerPhone',
          disabled: true
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'orderDate',
          initialValue: '',
          disabled: true,
          valueFormat: 'yyyy-MM-dd',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '申请备注',
          initialValue: '',
          fieldName: 'remark',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          initialValue: '',
          fieldName: 'examinedRemark',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '仓库名称',
          dataIndex: 'storageCode',
          initialValue: '',
          sorter: true
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          initialValue: '',
          sorter: true
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          initialValue: '',
          sorter: true
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          initialValue: '',
          sorter: true
        },
        {
          title: '调拨数量',
          dataIndex: 'inQuantity',
          initialValue: 0,
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number'
        },
        {
          title: '调拨单价',
          filterType: 'number',
          initialValue: 0,
          dataIndex: 'inPrice',
          precision: 2,
          sorter: true
        },
        {
          title: '调拨金额',
          initialValue: '',
          filterType: 'number',
          initialValue: 0,
          precision: 2,
          dataIndex: 'inAmount',
          initialValue: 0,
          sorter: true
        }
      ];
    },
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.topFilterList.find(item => item.fieldName === 'handler').itemList = data.map(x => ({ value: x.userId + '', text: x.employeeName }));
    },
    changeAmount(val, row) {
      console.log('val::', val, 'row::', row);
      row.inAmount = val[Object.keys(val)[0]] * row.inPrice;
      console.log('val1::', val, 'row::1', row);
    },
    collapseHandle() {
      this.refs.table.EXECUTE_RESET_HEIGHT();
    },
    init() {
      console.log(this.data, 'datainit');
      this.topFilterList.find(x => {
        x.initialValue = this.data[x.fieldName];
      });
      this.fetchParams = this.fetchParams = Object.assign({ allocateInNo: this.data.allocateInNo });
      this.fetchParams1 = this.fetchParams1 = Object.assign({ allocateInNo: this.data.allocateInNo });
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
